package com.xjoyt.pay.merchant.register.services;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xjoyt.pay.commons.PayTypeUtils;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.dto.PageParam;
import com.xjoyt.pay.dto.PageResult;
import com.xjoyt.pay.master.interfaces.MerchantRateContract;
import com.xjoyt.pay.master.vo.RateEntry;
import com.xjoyt.pay.merchant.register.params.MerchantRatePageParam;
import com.xjoyt.pay.merchant.register.po.Merchant;
import com.xjoyt.pay.merchant.register.po.MerchantRate;
import com.xjoyt.pay.merchant.register.po.MerchantRateDetail;
import com.xjoyt.pay.merchant.register.repository.MerchantRateDetailRepository;
import com.xjoyt.pay.merchant.register.repository.MerchantRateRepository;
import com.xjoyt.pay.merchant.register.repository.MerchantRepository;
import com.xjoyt.pay.vo.PayType;

@Service
public class MerchantRateService {

	@Autowired
	private MerchantRateRepository repository;

	@Autowired
	private MerchantRepository merchantRepository;

	@Autowired
	private MerchantRateDetailRepository merchantRateDetailRepository;

	/*@Autowired
	private MerchantRateContract merchanRatetContract;
*/
	public ExecuteResult<?> saveRate(com.xjoyt.pay.master.vo.MerchantRateMap param) {
		Merchant merchant = merchantRepository.findOne(param.id);
		if (merchant == null) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByMerchantId(param.id).forEach(item -> repository.delete(item));
		if (CollectionUtils.isEmpty(param.rateList)) {
			return ExecuteResult.ok();
		}
		MerchantRate entity = new MerchantRate();
		entity.merchant = merchant;
		repository.save(entity);
		for (RateEntry item : param.rateList) {
			if (StringUtils.isBlank(PayTypeUtils.getName(item.type)))
				continue;

			MerchantRateDetail merchantRateDetail = new MerchantRateDetail();
			merchantRateDetail.type = item.type;
			merchantRateDetail.rate = item.rate;
			merchantRateDetail.merchantRate = entity;
			merchantRateDetailRepository.save(merchantRateDetail);

		}

		return ExecuteResult.ok();
	}

	public ExecuteResult<MerchantRate> save(com.xjoyt.pay.master.po.MerchantRate param) {
		if (param.merchant == null || StringUtils.isBlank(param.merchant.id)) {
			return ExecuteResult.fail("数据不存在");
		}
		repository.findAllByMerchantId(param.merchant.id).forEach(item -> {
			repository.delete(item);
		});
		MerchantRate entity = new MerchantRate();
		entity.merchant = merchantRepository.findOne(param.merchant.id);
		repository.save(entity);

		for (PayType type : PayType.values()) {
			MerchantRateDetail merchantRateDetail = new MerchantRateDetail();
			merchantRateDetail.type = type;
			if (type.equals(param.type)) {
				merchantRateDetail.rate = param.rate;
				merchantRateDetail.type = param.type;
			}
			merchantRateDetail.merchantRate = entity;
			merchantRateDetailRepository.save(merchantRateDetail);
		}

		return ExecuteResult.ok(entity);
	}

	/*public PageResult<com.xjoyt.pay.merchant.register.vo.MerchantRate> loadPage(
			PageParam<MerchantRatePageParam> param) {
		return repository.loadPage(param);
	}*/

	public ExecuteResult<com.xjoyt.pay.merchant.register.po.Merchant> check(String id, Boolean enabled) {
		MerchantRate entity = repository.findOne(id);
		if (entity.enabled != null) {
			return ExecuteResult.fail("审核失败");
		}
		entity.enabled = enabled;
		repository.save(entity);
		/*if (enabled) {
			merchanRatetContract.update(entity);
		}*/
		return ExecuteResult.ok();
	}

}
